<?php
namespace app\controller;

use think\Lang;
use think\Db;
use think\Request;
use think\Config;

class Index extends Base
{    
    /**
     * 首页
     */
    public function index()
    {
        // 获取精选歌词
        $featured_lyrics = Db::name('lyrics')
            ->where('status', 1)
            ->where('language', $this->_current_lang)
            ->where('is_featured', 1)
            ->order('created_at desc')
            ->limit(6)
            ->select();
        
        // 获取最新歌词
        $latest_lyrics = Db::name('lyrics')
            ->where('status', 1)
            ->where('language', $this->_current_lang)
            ->order('created_at desc')
            ->limit(8)
            ->select();
        
        // 获取推荐搜索关键词
        $recommended_searches = Db::name('hot_searches')
            ->where('is_recommended', 1)
            ->where('language', $this->_current_lang)
            ->order('count desc')
            ->limit(5)
            ->select();

        $service = new \app\service\LyricsService();
        foreach($featured_lyrics as $key => $lyric){
            $description = $service->formatDescription($lyric['content']);
            $featured_lyrics[$key]['description'] = $description;
        }
        
        // 获取搜索进度条配置
        $search_progress_time = Config::get('app.search.progress_time') ?: 15;
                
        $this->assign('featured_lyrics', $featured_lyrics);
        $this->assign('latest_lyrics', $latest_lyrics);
        $this->assign('recommended_searches', $recommended_searches);
        $this->assign('search_progress_time', $search_progress_time);
        
        $friend_links = Db::name('friend_links')->select();
        $this->assign('friend_links', $friend_links);
        $this->assign('current_uri', "");
        $this->assign([
            'page_title' => Lang::get('seo_pages.index.title'),
            'page_description' => Lang::get('seo_pages.index.description'),
        ]);
        return $this->fetch();
    }
    
    /**
     * 歌词列表页
     */
    public function list(Request $request)
    {
        $sort = $request->param('sort', 'latest');
        
        // 构建查询条件
        $where = ['status' => 1];
        
        // 构建排序
        $order = [];
        switch ($sort) {
            case 'popular':
                $order = ['views' => 'desc', 'likes' => 'desc'];
                break;
            case 'az':
                $order = ['title' => 'asc'];
                break;
            case 'latest':
            default:
                $order = ['created_at' => 'desc'];
                break;
        }
        
        // 查询歌词列表
        $lyrics = Db::name('lyrics')
            ->where($where)
            ->where('language', $this->_current_lang)
            ->order($order)
            ->paginate(16);
        
        $this->assign('lyrics', $lyrics);
        $this->assign('page', $lyrics->render());
        $this->assign('sort', $sort);
        $this->assign([
            'page_title' => Lang::get('seo_pages.list.title'),
            'page_description' => Lang::get('seo_pages.list.description'),
        ]);
        $this->assign('current_uri', "list");
        
        return $this->fetch();
    }
    
    /**
     * 歌词详情页
     */
    public function detail($title)
    {
        $title = input('param.title', '', 'trim');
        if (empty($title)) {
            header('404 Not Found');
            throw new \think\exception\HttpException(404, '页面不存在');
        }
        
        // 查询歌词信息
        $lyric = Db::name('lyrics')
            ->where('title_uri', $title)
            ->where('language', $this->_current_lang)
            ->where('status', 1)
            ->find();
        
        // 如果歌词不存在，跳转到404页面
        if (empty($lyric)) {
            header('404 Not Found');
            throw new \think\exception\HttpException(404, '页面不存在');
        }
        $service = new \app\service\LyricsService();
        $description = $service->formatDescription($lyric['content']);
        
        // 查询相关歌词
        $related_lyrics = [];
        
        // 首先尝试查找同一艺术家的其他歌词
        if (!empty($lyric['artist'])) {
            $related_lyrics = Db::name('lyrics')
                ->where('artist', $lyric['artist'])
                ->where('id', '<>', $lyric['id'])
                ->where('status', 1)
                ->order('views desc')
                ->limit(5)
                ->select();
            
        }

        
        // 准备JavaScript语言变量
        $js_langs = [
            'lang_toggle_both' => Lang::get('lang_toggle_both'),
            'lang_toggle_pinyin' => Lang::get('lang_toggle_pinyin'),
            'lang_toggle_chinese' => Lang::get('lang_toggle_chinese'),
            'lang_toggle_switched_to_both' => Lang::get('lang_toggle_switched_to_both'),
            'lang_toggle_switched_to_pinyin' => Lang::get('lang_toggle_switched_to_pinyin'),
            'lang_toggle_switched_to_chinese' => Lang::get('lang_toggle_switched_to_chinese'),
        ];
        
        // 获取当前URL用于分享
        $current_url = '';
        
        $this->assign('lyric', $lyric);
        $this->assign('related_lyrics', $related_lyrics);
        $this->assign('current_url', $current_url);
        $this->assign('js_langs', json_encode($js_langs, JSON_UNESCAPED_UNICODE));

        $page_description = $description;
        $page_title_title = $lyric['title_pinyin'] . '(' . $lyric['title'] . ')';
        $page_title = Lang::get('seo_pages.detail.title',['title'=>$page_title_title ,'artist'=>$lyric['artist_pinyin']]);
        
        $this->assign([
            'page_title' => $page_title,
            'page_description' => $page_description,
        ]);
        $this->assign('current_uri', "lyrics");
        return $this->fetch();
    }
    
    /**
     * 关于页面
     */
    public function about()
    {        
        $this->assign('current_uri', "about");
        $this->assign([
            'page_title' => Lang::get('seo_pages.about.title'),
            'page_description' => Lang::get('seo_pages.about.description'),
        ]);
        return $this->fetch('about');
    }
    
    /**
     * 联系页面
     */
    public function contact()
    {
        $this->assign('current_uri', "contact");
        $this->assign([
            'page_title' => Lang::get('seo_pages.contact.title'),
            'page_description' => Lang::get('seo_pages.contact.description'),
        ]);
        return $this->fetch('contact');
    }
    
    /**
     * 隐私政策页面
     */
    public function privacy()
    {
        $this->assign('current_uri', "privacy");
        $this->assign([
            'page_title' => Lang::get('seo_pages.privacy.title'),
            'page_description' => Lang::get('seo_pages.privacy.description'),
        ]);
        return $this->fetch('privacy');
    }
    
    /**
     * 服务条款页面
     */
    public function terms()
    {
        $this->assign('current_uri', "terms");
        $this->assign([
            'page_title' => Lang::get('seo_pages.terms.title'),
            'page_description' => Lang::get('seo_pages.terms.description'),
        ]);
        return $this->fetch('terms');
    }
}
